#include <arpa/inet.h>
#include <google/protobuf/service.h>
#include <pthread.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#include "../rocket/common/config.h"
#include "../rocket/common/log.h"

#include "../rocket/common/log.h"
#include "../rocket/net/rpc/rpc_dispatcher.h"
#include "../rocket/net/tcp/net_addr.h"
#include "../rocket/net/tcp/tcp_server.h"
#include "order.pb.h"

class OrderImpl : public Order {
  public:
    void makeOrder(google::protobuf::RpcController *controller,
                   const ::makeOrderRequest *request,
                   ::makeOrderResponse *response,
                   ::google::protobuf::Closure *done) {
        sleep(1);
        if (request->price() < 10) {
            response->set_res_code(01);
            response->set_res_info("short balance");
            return;
        }
        response->set_order_id("20240120");
    }
};

void test_tcp_server() {

    rocket::IPNetAddr::s_ptr addr =
        std::make_shared<rocket::IPNetAddr>("127.0.0.1", 12345);

    DEBUGLOG("create the addr %s", addr->toString().c_str());

    rocket::TcpServer tcp_server(addr);
    tcp_server.start();
}

int main() {
    rocket::Config::SetGlobalConfig("conf/rocket.xml");
    rocket::Logger::InitGlobalLogger();
    std::shared_ptr<OrderImpl> service = std::make_shared<OrderImpl>();
    rocket::RpcDispatcher::GetRpcDispatcher()->registerService(service);

    test_tcp_server();

    return 0;
}
